<?php 
require_once 'dataapi.php';



class compile
{

    var $enginetables;
    var $db;
    public function __construct($config,$db)
    {
    	$this->db = $db;
        $this->enginetables = $config->enginetables;
       // var_dump($config->enginetables);
    
    }
    
    public function compile_description($entityid, $typeid, $blocknum = 1, $blockcount = 3)
    {
    	
        $tablename = $this->enginetables[$entityid];
        do
        {
            $set = $this->create_rnd_array($tablename, $blockcount,$typeid);
        }
        while(!$this->is_unique_set($set));
       echo $entityid;
        $text = '';
        if ($entityid == 'title')
        {
            var_dump($set);//die;
        }
        foreach($set as $index => $random)
        {  
        	$block = $this->db->select($tablename,array('typeid' => $typeid,'id' => $random));//var_dump($set);die;
        	//$all = ($tablename,array('typeid' => $typeid));
        	//$block = $all[$random];//var_dump($block);die;
        	$block = array_shift($block);//var_dump($block);
        	$text .= $block['text']. '<br/>';
        	//var_dump($random.' '.$typeid, ' '.$tablename);
        	
        }
        return $text;
        	 
    }

    private function create_rnd_array($table,$count = 3,$typeid = 2)
    {
        $allrecords = $this->db->select($table, array('typeid' => $typeid));
       // if ($table == 'titles')
        //	var_dump($allrecords);
        $set = array();
        for($i = 1;$i <= $count;$i++)
        {
        	$record = array_rand($allrecords);//var_dump($allrecords[$record]['id']);
            $set[$i] = $allrecords[$record]['id'];
        }
      // var_dump($set);die;
        return $set;
    }
    
    
    private function is_unique_set($set)
    {
        $tid = 0;
        $tnumber = 0;
        foreach ($set as $id => $number)
        {
            $tnumber = $number;
            $tid = $id;
            foreach ($set as $id => $number)
            {
            	if (($tnumber == $number)AND($id !== $tid))
            	{
            		return false;
            	}
            }	
        }
        return true;
        
    }
    
    public function make_deadline($entityid, $typeid, $offset = 3)
    {
    	$replace = '@';
    	$text = $this->compile_description($entityid, $typeid, 1,1);
    	$datetxt = $this->get_date();
    	$deadline = str_replace('@',$datetxt,$text);
    	return $deadline;
    	
    }
    
    private function get_date($offset = 5)
    {
    	
    	$date = date("m-d");
    	$datearr =explode('-', $date);
    	$monthnum = $datearr[0];
    	$day = $datearr[1] + $offset;
    	
    	switch ($monthnum)
    	{
    	    case '1': $month = 'января';break;
    	    case '2': $month = 'февраля';break;
    	    case '3': $month = 'марта';break;
    	    case '4': $month = 'апреля';break;
    	    case '5': $month = 'мая';break;
    	    case '6': $month = 'июня';break;
    	    case '7': $month = 'июля';break;
    	    case '8': $month = 'августа';break;
    	    case '9': $month = 'сентября';break;
    	    case '10': $month = 'октября';break;
    	    case '11': $month = 'ноября';break;
    	    case '12': $month = 'декабря';break;
    	    default: $month = 'января';break;
    	}
    	
    	$datestr = $day.' '.$month;
    	return $datestr;
    		
    }
    
    
    
    
    public function replace_sinonim($text, $word, $varioty = 1)
    {
    
    	$word = mb_convert_case($word, MB_CASE_LOWER, "UTF-8");
    	
    	$dictionary = $this->db->select('dictionary',array());
    	
    	foreach ($dictionary as $dset)
    	{
    		$sset = array();
    		//var_dump($dset);
    		foreach($dset as $id=>$v)
    		{
    			$dset[$id] = mb_convert_case($dset[$id], MB_CASE_LOWER, "UTF-8");
    			//var_dump($id);
    			//var_dump($v);
    		}
    		
    		if ((!in_array($word, $dset))&&(count($dset) > 1))
    		{
    			continue;
    		}
    		for ($i = 1; $i<=5;++$i)
    		{
    			if (($dset[$i] !== NULL)&&(!empty($dset[$i])))
    			{
    				$sset[] = $dset[$i];
    			}
    			
    		}
    		$repeat = mb_substr_count($text,$word);
    		for($i=1;$i<= $repeat; $i++)
    		{
    			$rand = rand(1,count($sset));$count=0;
    			//$replacedtext = str_replace($word,$sset[$rand-1],$text,$count);echo $word.'-'.$sset[$rand-1];
    			
    			$text = substr_replace($text, $sset[$rand-1], strpos($text, $word), strlen($word));
    
    		}
    		
    		
    	}
    	
    	return $text; 
    	
    }
    
    
    function full_replace($text)
    {
    	$readytext = $text;
    	$text = trim ((mb_convert_case($text, MB_CASE_LOWER, "UTF-8")));
    	$text = str_replace('!',' ',$text);
    	$text = str_replace('.',' ',$text);
    	$text = str_replace('?',' ',$text);
    	$text = str_replace(',',' ',$text);
    	$text = str_replace('!',' ',$text);
    	$text = str_replace(';',' ',$text);
    	$text = str_replace(';',' ',$text);
    	$text = strip_tags($text);
    	//str_replace('@',$datetxt,$text);
    	$words = explode(' ', $text);
    	$words = array_unique($words);
    	foreach($words as $id)
    	{
    		//$words[$id] = trim($words[$id]," \xC2\xA0");
    		/*if(strlen($words[$id]) < 10)
    		{
    			unset($words[$id]);
    		}   		*/
    	}
    	foreach($words as $id => $word)
    	{
    		$readytext = $this->replace_sinonim($readytext,$word);
		
    	}
    	return $readytext;
    	
    }
    
    public function get_email()
    {
    	$names = array(
    			'alex',
    			'alex_super',
                 'penetrator',
    			'ivan',
    			'sasha',
    			'huesos',
    			'pidr',
    			'nehuesos',
    			'mudak',
    			'gandon',
    			'anna',
    			'petr',
    			'vova',
    			'vovan',
    			'nastya',
    			'galya',
    			'galina',
    			'andrey'   			
    			);
    	$domains = array(
    			'ya.ru',
    			'yandex.ru',
                 'mail.ru',
    			'rambler.ru',
    			'yahoo.com',
    			'mail.i.ua'
    			);
    	$delimiters = array('','.','-','_');
    	$name = $names[rand(0,count($names)-1)];
    	
    	$timestamp = (string)time();
    	$delimiter = $delimiters[rand(0, count($delimiters)-1)];
    	$email = $name.$delimiter.(string)(rand(0,9)).substr($timestamp,rand(0,4),strlen($timestamp)).
    	        '@'.$domains[rand(0,count($domains)-1)];
    	return $email;
    }
    
    public function get_name()
    {
    	$sets = array(
    			'Александр',
    			'Алексей',
    			'Иван',
    			'Борис',
    			'Георгий',
    			'Григорий',
    			'Павел',
    			'Петр',
    			'Пётр',
    			'Михаил',
    			'Геннадий',
    			'Николай',
    			'Владимир',
    			'Василий',
    			'Вячеслав',
    			'Валерий',
    			'Никита',
    			'Дмитрий',
    			'Афанасий',
    			'Максим',
    			'Сергей'
    	);
    	
    	return $sets[rand(0, count($sets)-1)];
    }
    
    
    public function get_phone()
    {
    	$sets = array(
    			'9166603943',
    			'9161582612',
    			'9199610473',
    			'9857723572',
    			'9857723807',
    			'9857723919',
    			'9857726526',
    			'9266896420'
    	);
    	$codes = array('','+7','8'); 
    	return $codes[rand(0, count($codes)-1)].$sets[rand(0, count($sets)-1)];
    }
    
    public function get_title($thematic = 2)
    {
    	$title = $this->compile_description('title',$thematic,1,1);//var_dump($title);die;
    	return $title;
    }
    
    
    public function compile_full($thematic = 2)
    {
    	
    	
        if ($thematic == 2)
        {
        	$block1 = $this->compile_description(1,$thematic,1,rand(2,3));//print($a);
        	$block2 = $this->compile_description(2,$thematic,1,1);//print($b);
        	$block3 = $this->compile_description(4,$thematic,1,3);//print($d);
        	$deadline = $this->make_deadline(3,$thematic);//print($c);
        	$block4 = $this->compile_description(5,$thematic,1,1);//print($e);
        	$text = $block1.''.$block2.''.$block3.''.$deadline.''.$block4;
        	if ((rand(0, 2)) !== 0)
        	{
        		$deadline = '';
        	}
        	if ((rand (0,1)) == 0 )
        	{
        		$text = str_replace("<br/>","\n",$text);
        	}
        }
        
        if ($thematic == 3)
        {
        	$block1 = $this->compile_description(1,$thematic,1,rand(1,3));//print($a);
        	$block2 = $this->compile_description(2,$thematic,1,1);//print($b);
        	$block3 = $this->compile_description(4,2,1,3);//print($d);
        	$deadline = $this->make_deadline(3,2);//print($c);
        	$block4 = $this->compile_description(5,2,1,1);//print($e);
        	
        	if ((rand(0, 2)) !== 0)
        	{
        		$deadline = '';
        	}
        	$text = $block1.''.$block2.''.$block3.''.$deadline.''.$block4;
        	if ((rand (0,1)) == 0 )
        	{
        		
        		if((rand (0,1)) == 0 )
        	    {
        	    	$text = $block1.'-'.$block2.'-'.$block3.'-'.$deadline.'-'.$block4;
        	    }
        	    $text = str_replace("<br/>","\n",$text);
        	}
        	
        	
        	
        	
        }
        
        //if 
       
        
        return $text;
        
    }
    public function add_accounts($count = 5)
    {

    	for($i = 0;$i < $count;$i++)
    	{
    		$this->db->insert('accounts',array('name' => $this->get_name(),
    				'email' => $this->get_email()));
    	}
    }
    
    public function get_account()
    {

    	
    }
    
}


?>